#include <string>
#include <iostream>
using namespace std;

class LZW {
private:
    Dictionary inputDict;
    Dictionary outputDict;
    string w;
    string v;
public:
    int Encode(char k) {
        int temp;
        if (inputDict.findEntry("" + w + k) != -1) {
            w = w.append(k);
            throw new DoNotSend();
        } else {
            inputDict.addEntry("" + w + k);
            temp = inputDict.findEntry("" + w);
            w = new string("" + k);
            return temp;
        }
    }


    string Decode(int k) {
        string entry;

        if (v.length() == 0) {
            v = new string( outputDict.returnEntry(k) );
            return (v.tostring());
        } else {
            entry = new string( outputDict.returnEntry(k) );

            if (entry.length() == 0) {          // remove this conditional to cause LZW bug
                entry = new string( v.tostring() + v.charAt(0) );
            }

            outputDict.addEntry("" + v + entry.charAt(0) );

            v = new string(entry);
            return entry;
        }
    }
};